/**
 * 剑指 Offer 62. 圆圈中最后剩下的数字
 * https://leetcode-cn.com/problems/yuan-quan-zhong-zui-hou-sheng-xia-de-shu-zi-lcof/
 */

/**
 * @param {number} n
 * @param {number} m
 * @return {number}
 */
function lastRemaining(n, m) {
  let result = 0;
  for (let i = 2; i <= n; i += 1) {
    result = (result + m) % i;
  }
  return result;
}

console.log(lastRemaining(5, 3) === 3);
console.log(lastRemaining(10, 17) === 2);
console.log(lastRemaining(1, 1) === 0);
console.log(lastRemaining(82002, 120659) === 9966);
